package com.dlyk.web;

import com.dlyk.DlykServeApplication;
import com.dlyk.model.TActivity;
import com.dlyk.model.TDicType;
import com.dlyk.model.TDicValue;
import com.dlyk.model.TProduct;
import com.dlyk.query.DicTypeQuery;
import com.dlyk.result.DicEnum;
import com.dlyk.result.R;
import com.dlyk.service.DicValueService;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * @description:
 * @author: ajie
 * @date: 2024/7/27 20:21:04
 * @version: 1.0
 */
@RestController
public class DicValueController {

    @Resource
    private DicValueService dicValueService;

    @GetMapping("api/dicvalue/{typeCode}")
    private R dicData(@PathVariable(value = "typeCode") String typeCode){
        if (typeCode.equals(DicEnum.ACTIVITY.getCode())){
            //activity
            List<TActivity> tActivityList = (List<TActivity>) DlykServeApplication.cacheMap.get(typeCode);
            return R.OK(tActivityList);
        }else if (typeCode.equals(DicEnum.PRODUCT.getCode())){
            //product
            List<TProduct> tProductList = (List<TProduct>) DlykServeApplication.cacheMap.get(typeCode);
            return R.OK(tProductList);
        }else if (typeCode.equals(DicEnum.DICTYPE.getCode())){
            //dicType
            List<TDicType> tDicTypeList = (List<TDicType>) DlykServeApplication.cacheMap.get(typeCode);
            return R.OK(tDicTypeList);
        }else {
            List<TDicValue> tDicValueList = (List<TDicValue>) DlykServeApplication.cacheMap.get(typeCode);
            return R.OK(tDicValueList);
        }
    }

    /**
     * @description: 字典数据列表分页查询
     * @params: [current]
     * @return:
     * @author: ajie
     * @date: 2024/7/24 20:47
     */
    @GetMapping("api/dicValue")
    public R dicValuePage(@RequestParam(value = "current",required = false) Integer current,TDicValue tDicValue){
        if (current == null){
            current = 1;
        }
        PageInfo<TDicValue> pageInfo = dicValueService.getDicValueByPage(current,tDicValue);
        return R.OK(pageInfo);
    }

    /**
     * @description: 根据id查询字典数据
     * @params: [id]
     * @return:
     * @author: ajie
     * @date: 2024/7/27 23:54
     */
    @GetMapping("api/dicValue/detail/{id}")
    public R loadDicType(@PathVariable(value = "id") Integer id){
        TDicValue tDicValue = dicValueService.getDicValueById(id);
        return R.OK(tDicValue);
    }

    /**
     * @description: 新增字典数据
     * @params: [tDicType]
     * @return:
     * @author: ajie
     * @date: 2024/7/15 21:25
     */
    @PostMapping("api/dicValue")
    public R addDicValue(TDicValue tDicValue){
        int save = dicValueService.saveDicValue(tDicValue);
        return save >= 1 ? R.OK() : R.FAIL();
    }

    /**
     * @description: 编辑字典数据
     * @params: [tDicType]
     * @return:
     * @author: ajie
     * @date: 2024/7/15 21:25
     */
    @PutMapping("api/dicValue")
    public R editDicValue(TDicValue tDicValue){
        int update = dicValueService.updateDicValue(tDicValue);
        return update >= 1 ? R.OK() : R.FAIL();
    }

    /**
     * @description: 根据id删除字典数据
     * @params: [id]
     * @return:
     * @author: ajie
     * @date: 2024/7/31 18:16
     */
    @DeleteMapping("api/dicValue/{id}")
    public R delDicValue(@PathVariable(value = "id") Integer id){
        int del = dicValueService.delDicValueById(id);
        return del >= 1 ? R.OK() : R.FAIL();
    }

    /**
     * @description: 批量删除字典数据
     * @params: [ids]
     * @return:
     * @author: ajie
     * @date: 2024/7/31 18:32
     */
    @DeleteMapping("api/dicValue")
    public R batchDelDicType(@RequestParam(value = "ids") String ids){
        //ids = "1,3,5,7";
        List<String> idList = Arrays.asList(ids.split(","));
        int batchDel = dicValueService.batchDelDicValueByIds(idList);
        return batchDel >= idList.size() ? R.OK() : R.FAIL();
    }

    /**
     * 查询所有的线索来源
     *
     * @return
     */
    @GetMapping(value = "/api/source/all")
    public R allSource() {
        List<TDicValue> tDicValueList = dicValueService.getSourceByAll();
        return R.OK(tDicValueList);
    }
}
